package com.webservice.view.panel;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.webservice.ftp.utils.EncryptConstant;
import com.webservice.ftp.utils.FtpDataOper;
import com.webservice.ftp.utils.XmlUtil;

/**
 * 文本解密TabPanel页面
 * 
 * @author Administrator
 * 
 */
public class TextEncryptionPanel extends JPanel implements EncryptConstant {

	private static final long serialVersionUID = 2616998568109305698L;

	private JTextArea beforeDecryptArea;
	private JButton encryptButton;
	private JButton decryptButton;
	private JTextArea afterDecryptArea;
	
	public TextEncryptionPanel() {
		initComponents();
		initListener();
	}

	/**
	 * 初始化组件
	 */
	private void initComponents() {
		setLayout(new GridBagLayout());
		
		beforeDecryptArea = new JTextArea();
		JScrollPane beforeDecryptPane = new JScrollPane(beforeDecryptArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		
		Box mainBox = Box.createHorizontalBox();
		
		encryptButton = new JButton("加密");
		encryptButton.setEnabled(false);
		decryptButton = new JButton("解密");
		
		mainBox.add(encryptButton);
		mainBox.add(Box.createHorizontalStrut(10));
		mainBox.add(decryptButton);
		
		afterDecryptArea = new JTextArea();
//		afterDecryptArea.setEnabled(false);
		JScrollPane afterDecryptPane = new JScrollPane(afterDecryptArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		
		add(beforeDecryptPane, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
				GridBagConstraints.WEST, GridBagConstraints.BOTH,
				new Insets(10, 10, 0, 10), 0, 0));
		add(mainBox, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.NONE,
				new Insets(10, 10, 10, 10), 0, 0));
		add(afterDecryptPane, new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0,
				GridBagConstraints.WEST, GridBagConstraints.BOTH,
				new Insets(0, 10, 10, 10), 0, 0));
	}

	/**
	 * 初始化监听事件
	 */
	private void initListener() {
		encryptButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
			}
		});
		decryptButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String content = beforeDecryptArea.getText().trim();
				String xmlData = new FtpDataOper().ftpFileOper(content, aesKey,
						aesOffsets, hashcode, messageKey);
				
				try {
					String formatXML = XmlUtil.formatXML(xmlData);
					afterDecryptArea.setText(formatXML);
				} catch (Exception e1) {
					afterDecryptArea.setText(e1.getMessage());
				}
				
			}
		});
	}

}
